package com.example.lp.lpmvvm.one.view;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.lp.lpglide.LpGlide;
import com.example.lp.lpglide.Request.RequestListener;
import com.example.lp.lpmvp.R;
import com.example.lp.lpmvp.databinding.PictureViewBinding;
import com.example.lp.lpmvvm.one.viewmodel.BaseViewModel;
import com.example.lp.lpmvvm.one.viewmodel.IBaseView;

/**
 * Author by lp,on 2021/6/15/015,17:44.
 */
public abstract class BaseMvvmView<VIEW_MODEL extends BaseViewModel, DATA_BINDING extends ViewDataBinding> extends LinearLayout implements IBaseView<VIEW_MODEL> {
    private static String TAG = "PictureView";
    private DATA_BINDING binding;
    private VIEW_MODEL view_model;
    private Context context;

    public BaseMvvmView(Context context) {
        super(context);
        this.context = context;
        init(context);
    }

    public BaseMvvmView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public BaseMvvmView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public BaseMvvmView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        binding = DataBindingUtil.inflate(inflater, getViewLayoutId(), this, false);
        binding.getRoot().setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                RootClick(v);
            }
        });
        addView(binding.getRoot());
    }

    @Override
    public void setData(VIEW_MODEL viewModel) {
        this.view_model = viewModel;
        setDataToView(viewModel);
        if (binding != null) {
            binding.executePendingBindings();
        }
    }

    protected View  getView() {
        return binding.getRoot();
    }
    protected DATA_BINDING getDataBinding() {
        return binding;
    }

    protected VIEW_MODEL getViewModel() {
        return view_model;
    }

    protected abstract void setDataToView(VIEW_MODEL viewModel);

    protected abstract int getViewLayoutId();

    protected abstract void RootClick(View view);


}
